﻿namespace OutLook.UI.FrontEnd
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Globalization;
    using OutLook.Localization;
    using OutLook.Model.Infrastructure;
    using OutLook.Module.Load;
    using OutLook.Model.Domains;
    using OutLook.Module.Charting;
    using OutLook.Module.Infrastructure;
    using System.Linq;

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            UpdateLanguageMenus();

            m_OpenDialog.Filter = Constants.DIALOG_FILTER;
            m_OpenDialog.Title = Constants.OPEN_DIALOG_TITLE;
            m_SaveDialog.Filter = Constants.DIALOG_FILTER;
            m_SaveDialog.Title = Constants.SAVE_DIALOG_TITLE;

            InitializeEnthalpyChart();
            InitializeMetalSlagChart();
            InitializeSlagPropertiesChart();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdateLanguageMenus()
        {
            CultureInfo culture = CultureManager.ApplicationUICulture;
            englishToolStripMenuItem.Checked = (culture.TwoLetterISOLanguageName == "en");
            russianToolStripMenuItem.Checked = (culture.TwoLetterISOLanguageName == "ru");
        }

        private void englishToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CultureManager.ApplicationUICulture = new CultureInfo("en-US");
        }

        private void frenchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CultureManager.ApplicationUICulture = new CultureInfo("ru-RU");
        }

        private void cultureManager_UICultureChanged(CultureInfo newCulture)
        {
            UpdateLanguageMenus();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_OpenDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Load load;
                    Technology technoligy;
                    ElementsDistribution elemDistr;
                    Model model;
                    XlsToXml.ConvertXlsToXml(m_OpenDialog.FileName, out load, out technoligy, out elemDistr,out model);
                    LoadPackage.Instance.CurrentLoad = load;
                    LoadPackage.Instance.CurrentTechnology = technoligy;
                    FillLoadDataGrid(LoadPackage.Instance.GetLoadAtTable);
                    //FillLoadDataGrid(LoadPackage.Instance.DownloadTheLoad(m_OpenDialog.FileName));
                    DataTable dataTableTechnology = LoadPackage.Instance.GetTechnologyAsTable;//LoadPackage.Instance.DownloadTheTechnology(LoadPackage.Instance.CurrentLoadFileName);

                    if (dataTableTechnology == null)
                        MessageBox.Show(ResourceInformation.MainPage.WARNING_TECHNOLOGY, ResourceInformation.MainPage.WARNING);
                    else
                    {
                        FillTechnologyDataGrid(dataTableTechnology);
                    }

                    FillChargeIndicatorsDataGrid(LoadPackage.Instance.ChargeIndicators());
                    FillChargePropertiesDataGrid(LoadPackage.Instance.ChargeProperties());

                    LoadPackage.Instance.SetOutput(elemDistr,model);

                    FillSharesDataGrid(LoadPackage.Instance.CurrentOutput.LElemnets);
                    FillIronCastDataGrid(LoadPackage.Instance.GetCastIron());
                    FillSlagDataGrid(LoadPackage.Instance.GetSlag());

                    FillSlagPropertiesDataGrid(LoadPackage.Instance.GetSlagProperties());

                    InitializeEnthalpyChart();
                    InitializeMetalSlagChart();
                    InitializeSlagPropertiesChart();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ResourceInformation.MainPage.OPEN_ERROR_MSG, ResourceInformation.MainPage.OPEN_ERROR);
                }

            }
        }

        private void FillLoadDataGrid(DataTable dataTable)
        {
            DataGrid_Load.RowHeadersVisible = true;
            DataGrid_EditLoad.RowHeadersWidth = 5;
            BS_Load.DataSource = dataTable;

            DataGrid_Load.DataSource = BS_Load;
            // Resize the DataGridView columns to fit the newly loaded content.
            DataGrid_Load.AutoResizeColumns(
                DataGridViewAutoSizeColumnsMode.AllCells);
            DataGrid_Load.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);

            DataGrid_Load.Columns[0].Width = 40;
            DataGrid_Load.Columns[2].Width = 100;            
        }

        private void FillLoadEditDataGrid(DataTable dataTable)
        {
            DataGrid_EditLoad.Rows.Clear();
            DataGrid_EditLoad.Columns.Clear();
            DataGrid_EditLoad.RowHeadersVisible = true;
            DataGrid_EditLoad.RowHeadersWidth = 60;

            for (int i = 1; i < dataTable.Columns.Count; i++)
            {
                DataGrid_EditLoad.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].Caption);
                DataGrid_EditLoad.Columns[i - 1].Width = 80;
            }

            DataGrid_EditLoad.Columns[0].Width = 200;
            DataGrid_EditLoad.Columns[1].Width = 150;

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataGrid_EditLoad.Rows.Add();
                for (int j = 1; j < dataTable.Columns.Count; j++)
                {
                    DataGrid_EditLoad.Rows[i].Cells[j - 1].Value = dataTable.Rows[i][j].ToString();
                }

                DataGrid_EditLoad.Rows[i].Height = 30;
                DataGrid_EditLoad.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
        }

        private void FillTechnologyDataGrid(DataTable dataTable)
        {
            DataGrid_Technology.Rows.Clear();
            DataGrid_Technology.Columns.Clear();
            DataGrid_Technology.RowHeadersVisible = true;
            DataGrid_Technology.RowHeadersWidth = 60;

            for (int i = 1; i < dataTable.Columns.Count; i++)
            {
                DataGrid_Technology.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].Caption);
            }

            DataGrid_Technology.Columns[0].Width = 500;
            DataGrid_Technology.Columns[1].Width = 200;

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataGrid_Technology.Rows.Add();
                for (int j = 1; j < dataTable.Columns.Count; j++)
                {
                    DataGrid_Technology.Rows[i].Cells[j - 1].Value = dataTable.Rows[i][j].ToString();
                }

                DataGrid_Technology.Rows[i].Height = 30;
                DataGrid_Technology.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
        }

        private void FillSlagPropertiesDataGrid(DataTable dataTable)
        {
            DataGrid_SlagProperties.Rows.Clear();
            DataGrid_SlagProperties.Columns.Clear();
            DataGrid_SlagProperties.RowHeadersVisible = true;
            DataGrid_SlagProperties.RowHeadersWidth = 60;

            for (int i = 1; i < dataTable.Columns.Count; i++)
            {
                DataGrid_SlagProperties.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].Caption);
            }

            DataGrid_SlagProperties.Columns[0].Width = 500;
            DataGrid_SlagProperties.Columns[1].Width = 200;

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataGrid_SlagProperties.Rows.Add();
                for (int j = 1; j < dataTable.Columns.Count; j++)
                {
                    DataGrid_SlagProperties.Rows[i].Cells[j - 1].Value = dataTable.Rows[i][j].ToString();
                }

                DataGrid_SlagProperties.Rows[i].Height = 30;
                DataGrid_SlagProperties.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
        }

        private void FillChargeIndicatorsDataGrid(DataTable dataTable)
        {
            DataGrid_ChargeIndicators.Rows.Clear();
            DataGrid_ChargeIndicators.Columns.Clear();
            DataGrid_ChargeIndicators.RowHeadersVisible = false;

            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                DataGrid_ChargeIndicators.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].Caption);
                DataGrid_ChargeIndicators.Columns[i].Width = 80;
            }

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataGrid_ChargeIndicators.Rows.Add();
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    DataGrid_ChargeIndicators.Rows[i].Cells[j].Value = dataTable.Rows[i][j].ToString();
                }

                DataGrid_ChargeIndicators.Rows[i].Height = 30;
            }
        }

        private void FillChargePropertiesDataGrid(DataTable dataTable)
        {
            DataGrid_ChargeProperties.Rows.Clear();
            DataGrid_ChargeProperties.Columns.Clear();
            DataGrid_ChargeProperties.RowHeadersVisible = false;

            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                DataGrid_ChargeProperties.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].Caption);
                DataGrid_ChargeProperties.Columns[i].Width = 100;
            }

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataGrid_ChargeProperties.Rows.Add();
                for (int j = 0; j < dataTable.Columns.Count; j++)
                {
                    DataGrid_ChargeProperties.Rows[i].Cells[j].Value = dataTable.Rows[i][j].ToString();
                }

                DataGrid_ChargeProperties.Rows[i].Height = 30;
            }
        }

        private void FillIronCastDataGrid(DataTable dataTable)
        {
            DataGrid_IronCast.Rows.Clear();
            DataGrid_IronCast.Columns.Clear();
            DataGrid_IronCast.RowHeadersVisible = false;

            DataGrid_IronCast.Columns.Add(dataTable.Columns[dataTable.Columns.Count - 1].ColumnName, dataTable.Columns[dataTable.Columns.Count - 1].Caption);
            DataGrid_IronCast.Columns[0].Width = 150;

            for (int i = 1; i < dataTable.Columns.Count - 1; i++)
            {
                DataGrid_IronCast.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].Caption + " (%)");
                DataGrid_IronCast.Columns[i].Width = 100;
            }

            DataGrid_IronCast.Rows.Add();
            DataGrid_IronCast.Rows[0].Cells[0].Value = dataTable.Rows[0][dataTable.Columns.Count - 1].ToString();

            for (int j = 1; j < dataTable.Columns.Count - 1; j++)
            {
                DataGrid_IronCast.Rows[0].Cells[j].Value = dataTable.Rows[1][j].ToString();
            }

            DataGrid_IronCast.Rows[0].Height = 30;

        }

        private void FillSharesDataGrid(List<Coupla> L)
        {
            DataGrid_Shares.Rows.Clear();
            DataGrid_Shares.Columns.Clear();
            DataGrid_Shares.RowHeadersVisible = false;


            for (int i = 0; i < L.Count; i++)
            {
                DataGrid_Shares.Columns.Add(L[i].Indicator, L[i].Indicator);
                DataGrid_Shares.Columns[i].Width = 100;
            }

            DataGrid_Shares.Rows.Add();

            for (int j = 0; j < L.Count; j++)
            {
                DataGrid_Shares.Rows[0].Cells[j].Value = L[j].Value.ToString();
            }

            DataGrid_Shares.Rows[0].Height = 30;
        }

        private void FillSlagDataGrid(DataTable dataTable)
        {
            DataGrid_Slag.Rows.Clear();
            DataGrid_Slag.Columns.Clear();
            DataGrid_Slag.RowHeadersVisible = false;

            DataGrid_Slag.Columns.Add(dataTable.Columns[dataTable.Columns.Count - 1].ColumnName, dataTable.Columns[dataTable.Columns.Count - 1].Caption);
            DataGrid_Slag.Columns[0].Width = 150;

            for (int i = 1; i < dataTable.Columns.Count - 1; i++)
            {
                DataGrid_Slag.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[i].Caption + " (%)");
                DataGrid_Slag.Columns[i].Width = 110;
            }

            DataGrid_Slag.Rows.Add();
            DataGrid_Slag.Rows[0].Cells[0].Value = dataTable.Rows[0][dataTable.Columns.Count - 1].ToString();

            for (int j = 1; j < dataTable.Columns.Count - 1; j++)
            {
                DataGrid_Slag.Rows[0].Cells[j].Value = dataTable.Rows[1][j].ToString();
            }

            DataGrid_Slag.Rows[0].Height = 30;

        }

        #region Fields

        private OpenFileDialog m_OpenDialog = new OpenFileDialog();
        private SaveFileDialog m_SaveDialog = new SaveFileDialog();
        private Charge m_Charge = Charge.Create();
        private List<ChemicalСombination> m_ChargeList = new List<ChemicalСombination>();
        private ChemicalСombination m_Component = new ChemicalСombination();
        private LoadController m_LoadController = new LoadController(null);

        #endregion

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void Tb_Title_TextChanged(object sender, EventArgs e)
        {
            m_Charge = Charge.Create(Tb_Title.Text, m_ChargeList, m_Charge.Wet, m_Charge.Volatiles, m_Charge.Z);
            FillChargeDataGrid();
        }

        private void FillChargeDataGrid()
        {
            DataGrid_Charge.Rows.Clear();
            DataGrid_Charge.Columns.Clear();
            DataGrid_Charge.RowHeadersVisible = false;

            DataGrid_Charge.Columns.Add(ResourceInformation.MainPage.TITLE, ResourceInformation.MainPage.TITLE);
            DataGrid_Charge.Columns[0].Width = 150;

            for (int i = 0; i < m_Charge.Compositions.Count; i++)
            {
                DataGrid_Charge.Columns.Add(m_Charge.Compositions[i].Element, m_Charge.Compositions[i].Element + " (%)");
                DataGrid_Charge.Columns[i + 1].Width = 90;
            }

            DataGrid_Charge.Columns.Add(ResourceInformation.MainPage.WET, ResourceInformation.MainPage.WET);
            DataGrid_Charge.Columns[DataGrid_Charge.Columns.Count - 1].Width = 100;
            DataGrid_Charge.Columns.Add(ResourceInformation.MainPage.VOLATILES, ResourceInformation.MainPage.VOLATILES);
            DataGrid_Charge.Columns[DataGrid_Charge.Columns.Count - 1].Width = 100;
            DataGrid_Charge.Columns.Add(ResourceInformation.MainPage.Z, ResourceInformation.MainPage.Z);
            DataGrid_Charge.Columns[DataGrid_Charge.Columns.Count - 1].Width = 100;

            DataGrid_Charge.Rows.Add();
            DataGrid_Charge.Rows[0].Cells[0].Value = m_Charge.Title;

            for (int j = 0; j < m_Charge.Compositions.Count; j++)
            {
                DataGrid_Charge.Rows[0].Cells[j + 1].Value = m_Charge.Compositions[j].Percentage.ToString();
            }

            DataGrid_Charge.Rows[0].Cells[DataGrid_Charge.Columns.Count - 3].Value = m_Charge.Wet;
            DataGrid_Charge.Rows[0].Cells[DataGrid_Charge.Columns.Count - 2].Value = m_Charge.Volatiles;
            DataGrid_Charge.Rows[0].Cells[DataGrid_Charge.Columns.Count - 1].Value = m_Charge.Z;

            DataGrid_Charge.Rows[0].Height = 30;
        }

        private void TB_Wet_TextChanged(object sender, EventArgs e)
        {
            double wet = default(double);

            if (double.TryParse(TB_Wet.Text, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out wet))
                m_Charge.Wet = wet;
            else
                m_Charge.Wet = default(double);

            FillChargeDataGrid();
        }

        private void TB_Volatiles_TextChanged(object sender, EventArgs e)
        {
            double volatiles = default(double);

            if (double.TryParse(TB_Volatiles.Text, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out volatiles))
                m_Charge.Volatiles = volatiles;
            else
                m_Charge.Volatiles = default(double);

            FillChargeDataGrid();
        }

        private void TB_Z_TextChanged(object sender, EventArgs e)
        {
            double z = default(double);

            if (double.TryParse(TB_Z.Text, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out z))
                m_Charge.Z = z;
            else
                m_Charge.Z = default(double);

            FillChargeDataGrid();
        }

        private void Tb_Element_TextChanged(object sender, EventArgs e)
        {
            double percentage = default(double);
            double.TryParse(Tb_Percentage.Text, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out percentage);
            m_Component = new ChemicalСombination(Tb_Element.Text, percentage);
        }

        private void Tb_Percentage_TextChanged(object sender, EventArgs e)
        {
            double percentage = default(double);

            if (double.TryParse(Tb_Percentage.Text, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out percentage))
                m_Component.Percentage = percentage;
            else
                m_Component.Percentage = default(double);
        }

        private void Btn_AddComponent_Click(object sender, EventArgs e)
        {
            if (!m_Component.Element.Equals(string.Empty) && !m_ChargeList.Select(el => el.Element).Contains(m_Component.Element))
            {
                m_ChargeList.Add(m_Component);
                Charge charge = m_Charge;
                m_Charge = Charge.Create(charge.Title, m_ChargeList, charge.Wet, charge.Volatiles, charge.Z);
            }

            m_Component = new ChemicalСombination();

            Tb_Element.Text = string.Empty;
            Tb_Percentage.Text = string.Empty;

            FillChargeDataGrid();
        }

        private void Btn_CreateCharge_Click(object sender, EventArgs e)
        {
            if (!m_Charge.Title.Equals(string.Empty) && !m_LoadController.CurrentLoad.LoadInfo.ChargeToLoad.Select(item => item.ChargeRow.Title).Contains(m_Charge.Title))
                m_LoadController.CurrentLoad.LoadInfo.ChargeToLoad.Add(ChargeRowToLoad.Create(m_Charge, default(double)));

            FillLoadEditDataGrid(m_LoadController.GetLoadAtTable);

            m_Charge = Charge.Create();

            DataGrid_Charge.Rows.Clear();
            DataGrid_Charge.Columns.Clear();
        }

        private void TabItem_CreateLoad_Click(object sender, EventArgs e)
        {
            m_LoadController.CurrentLoad = new Load();
        }

        #region Charts Initialization

        private void InitializeEnthalpyChart()
        {
            chart1.Series.Clear();
            chart1.Series.Add(ResourceInformation.MainPage.EnthalpyChartSeriesCurveTitle);
            chart1.Series.Add("");
            chart1.Series.Add("");
            chart1.Series.Add("");
            chart1.Series[0].BorderWidth = 2;
            chart1.Series[1].BorderWidth = 2;
            chart1.Series[2].BorderWidth = 2;
            chart1.Series[3].BorderWidth = 5;
            chart1.Series[0].Color = System.Drawing.Color.Blue;
            chart1.Series[1].Color = System.Drawing.Color.Purple;
            chart1.Series[2].Color = System.Drawing.Color.Purple; 
            chart1.Series[3].Color = System.Drawing.Color.Orange;
            chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series[2].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series[3].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;

            foreach (var item in ChartController.GetEnthalpyData())
            {
                chart1.Series[0].Points.AddXY(item.Key, item.Value);
            }


            chart1.Series[1].Points.AddXY(ChartCharacteristics.DEL, 1800);
            chart1.Series[1].Points.AddXY(ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEL).Key, ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEL).Value);

            chart1.Series[2].Points.AddXY(ChartCharacteristics.DEP, 1800);
            chart1.Series[2].Points.AddXY(ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEP).Key, ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEP).Value);

            chart1.Series[3].Points.AddXY(ChartCharacteristics.DEP, 1800);
            chart1.Series[3].Points.AddXY(ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEP).Key, ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEP).Value);
            chart1.Series[3].Points.AddXY(ChartCharacteristics.DEL, 1800);
            chart1.Series[3].Points.AddXY(ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEL).Key, ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEL).Value);

        }

        private void InitializeMetalSlagChart()
        {
            chart2.Series.Clear();
            chart2.Series.Add("");
            chart2.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart2.Series[0].BorderWidth = 2;

            foreach (var item in ChartController.GetMetalSlagData())
            {
                chart2.Series[0].Points.AddXY(item.Key, item.Value);
            }
        }

        private void InitializeSlagPropertiesChart()
        {
            chart3.Series.Clear();
            chart3.Series.Add(ResourceInformation.MainPage.SlagPropertiesChartTitleY2);
            chart3.Series.Add(ResourceInformation.MainPage.SlagPropertiesChartTitleY1);

            chart3.Series[0].BorderWidth = 2;
            chart3.Series[1].BorderWidth = 2;

            chart3.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            chart3.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;

            chart3.Series[0].Color = System.Drawing.Color.Green;
            chart3.Series[1].Color = System.Drawing.Color.Red;

            foreach (var item in ChartController.GetMetalSlagData())
            {
                chart3.Series[0].Points.AddXY(item.Key, item.Value);
            }

            foreach (var item in ChartController.GetSlagPropertiesY2Data())
            {
                chart3.Series[1].Points.AddXY(item.Value, item.Key);
            }
        }

        #endregion

        private void TabControl_Graphics_SelectedTabChanged(object sender, DevComponents.DotNetBar.SuperTabStripSelectedTabChangedEventArgs e)
        {

        }

        private void saveMenuItem_Click(object sender, EventArgs e)
        {
            if (m_SaveDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    XlsToXml.ConvertXmlToXls(LoadPackage.Instance.CurrentOutput, m_SaveDialog.FileName);
                    //DataSerializerHelper.XmlWrite<Load>(LoadPackage.Instance.CurrentLoad, m_SaveDialog.FileName);
                }
                catch
                {
                    MessageBox.Show(ResourceInformation.MainPage.SAVE_ERROR_MSG, ResourceInformation.MainPage.SAVE_ERROR);
                }
            }
        }

        private void Btn_ClearCharge_Click(object sender, EventArgs e)
        {
            m_Component = new ChemicalСombination();
            m_ChargeList = new List<ChemicalСombination>();
            m_Charge = Charge.Create();

            Tb_Element.Text = string.Empty;
            Tb_Percentage.Text = string.Empty;

            Tb_Title.Text = string.Empty;
            TB_Volatiles.Text = string.Empty;
            TB_Wet.Text = string.Empty;
            TB_Z.Text = string.Empty;

            DataGrid_Charge.Rows.Clear();
            DataGrid_Charge.Columns.Clear();
        }

        private void DataGrid_EditLoad_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            for (int i = 0; i < DataGrid_EditLoad.SelectedRows.Count; i++)
            {
                m_LoadController.CurrentLoad.LoadInfo.ChargeToLoad.Remove(m_LoadController.CurrentLoad.LoadInfo.ChargeToLoad.Select(el => el).Where(el => el.ChargeRow.Title.Equals(DataGrid_EditLoad.SelectedRows[i].Cells[0])).First());
            }
        }

        private void Btn_SetAsCurrent_Click(object sender, EventArgs e)
        {
            LoadData loadInfo = LoadData.Create();
            List<ChargeRowToLoad> m_charges = new List<ChargeRowToLoad>();
            Charge charge;
            List<ChemicalСombination> composition;

            //if (!TB_LoadName.Text.Equals(string.Empty) && !string.IsNullOrWhiteSpace(TB_LoadName.Text))
            //{
            //    for (int i = 0; i < DataGrid_EditLoad.Rows.Count; i++)
            //    {
            //        composition = new List<ChemicalСombination>();

            //        for (int j = 2; j < DataGrid_EditLoad.Columns.Count - 3; j++)
            //        {
            //            double value = 0;
            //            if (double.TryParse(DataGrid_EditLoad.Rows[i].Cells[j].Value.ToString(), NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out value) && value != 0)
            //            {
            //                composition.Add(new ChemicalСombination(DataGrid_EditLoad.Columns[j].HeaderText, value));
            //            }
            //            else
            //            {
            //                MessageBox.Show(ResourceInformation.MainPage.FORMAT_ERROR, ResourceInformation.MainPage.WARNING);
            //                return;
            //            }
            //        }

            //        double weight = 0, wet = 0, volatiles = 0, z = 0;
            //        if (!double.TryParse(DataGrid_EditLoad.Rows[i].Cells[1].Value.ToString(), NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out weight))
            //        {
            //            MessageBox.Show(ResourceInformation.MainPage.FORMAT_ERROR, ResourceInformation.MainPage.WARNING);
            //            return;
            //        }

            //        if (!double.TryParse(DataGrid_EditLoad.Rows[i].Cells[DataGrid_EditLoad.Columns.Count - 3].Value.ToString(), NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out z))
            //        {
            //            MessageBox.Show(ResourceInformation.MainPage.FORMAT_ERROR, ResourceInformation.MainPage.WARNING);
            //            return;
            //        }

            //        if (!double.TryParse(DataGrid_EditLoad.Rows[i].Cells[DataGrid_EditLoad.Columns.Count - 2].Value.ToString(), NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out wet))
            //        {
            //            MessageBox.Show(ResourceInformation.MainPage.FORMAT_ERROR, ResourceInformation.MainPage.WARNING);
            //            return;
            //        }

            //        if (!double.TryParse(DataGrid_EditLoad.Rows[i].Cells[DataGrid_EditLoad.Columns.Count - 1].Value.ToString(), NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out volatiles))
            //        {
            //            MessageBox.Show(ResourceInformation.MainPage.FORMAT_ERROR, ResourceInformation.MainPage.WARNING);
            //            return;
            //        }

            //        charge = Charge.Create(DataGrid_EditLoad.Rows[i].Cells[0].Value.ToString(), composition, wet, volatiles, z);
            //        m_charges.Add(ChargeRowToLoad.Create(charge, weight));
            //    }

            //    LoadPackage.Instance.CurrentLoad = new Load(LoadData.Create(TB_LoadName.Text, m_charges, "t", 0));

            //    FillLoadDataGrid(LoadPackage.Instance.GetLoadAtTable);

            //    FillChargeIndicatorsDataGrid(LoadPackage.Instance.ChargeIndicators());
            //    FillChargePropertiesDataGrid(LoadPackage.Instance.ChargeProperties());

            //    LoadPackage.Instance.SetOutput(elemDistr);

            //    FillIronCastDataGrid(LoadPackage.Instance.GetCastIron());
            //    FillSlagDataGrid(LoadPackage.Instance.GetSlag());
            //}

            //DataGrid_EditLoad.Columns.Clear();
            //DataGrid_EditLoad.Rows.Clear();

        }

        private void Btn_xls_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog browser = new FolderBrowserDialog();
            if (browser.ShowDialog() == DialogResult.OK)
            {
                TB_xls.Text = browser.SelectedPath;
            }
        }

        private void Btn_xml_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog browser = new FolderBrowserDialog();
            if (browser.ShowDialog() == DialogResult.OK)
            {
                TB_xml.Text = browser.SelectedPath;
            }
        }

        private void DataGrid_Load_CellErrorTextChanged(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void DataGrid_Load_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
            }
            catch
            {
            }
        }

    }
}